<?php

require_once 'include/global.inc.php';

// remove any previous game information
// just so it doesn't cause problems
$_SESSION['gamedata'] = null;
unset($_SESSION['gamedata']);

// do we want to play a game ?
if (isset($_POST['game_id']) && (0 != $_POST['game_id'])){
	$_SESSION['game_id'] = sani($_POST['game_id']);

	$_SESSION['spectator'] = false;
	if (isset($_POST['spec']) && ('s' == $_POST['spec'])){
		$_SESSION['spectator'] = true;
	}

	call('GLOBALS');
	header('Location: play.php');
	exit;
}

// get the list of games for the players game list
$query = "
	SELECT G.*
		, P1.p_username AS black_username
		, P2.p_username AS white_username
	FROM ".T_GAME." AS G
		LEFT JOIN ".T_PLAYER." AS P1
			ON P1.p_id = G.g_black_player_id
		LEFT JOIN ".T_PLAYER." AS P2
			ON P2.p_id = G.g_white_player_id
	WHERE (
			g_white_player_id = '{$_SESSION['player_id']}'
			OR g_black_player_id = '{$_SESSION['player_id']}'
		)
		AND (
			g_status = 'Black'
			OR g_status = 'White'
		)
	ORDER BY g_id
";
$games = $mysql->fetch_array($query, __LINE__, __FILE__);

// get the list of other games for the list
$query = "
	SELECT G.*
		, P1.p_username AS black_username
		, P2.p_username AS white_username
	FROM ".T_GAME." AS G
		LEFT JOIN ".T_PLAYER." AS P1
			ON P1.p_id = G.g_black_player_id
		LEFT JOIN ".T_PLAYER." AS P2
			ON P2.p_id = G.g_white_player_id
	WHERE (
			g_white_player_id != '{$_SESSION['player_id']}'
			AND g_black_player_id != '{$_SESSION['player_id']}'
		)
		AND (
			g_status = 'Black'
			OR g_status = 'White'
		)
	ORDER BY g_num_moves
";
$other_games = $mysql->fetch_array($query, __LINE__, __FILE__);

// get the list of finished games for the list
$query = "
	SELECT G.*
		, P1.p_username AS black_username
		, P2.p_username AS white_username
	FROM ".T_GAME." AS G
		LEFT JOIN ".T_PLAYER." AS P1
			ON P1.p_id = G.g_black_player_id
		LEFT JOIN ".T_PLAYER." AS P2
			ON P2.p_id = G.g_white_player_id
	WHERE (
			g_status != 'Black'
			AND g_status != 'White'
		)
	ORDER BY g_last_move DESC
";
$finished_games = $mysql->fetch_array($query, __LINE__, __FILE__);

// start the page
echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n";
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
	"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
	<title>iShudan : Main Menu</title>

	<meta http-equiv="Content-Language" content="en-us" />
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
	<meta http-equiv="Content-Style-Type" content="text/css" />

	<link rel="stylesheet" type="text/css" href="css/iShudan.css" />

	<script type="text/javascript" src="javascript/game.js"></script>

</head>
<body>
<div id="links"><?php echo $CFG_SITELINKS; ?></div>
<h1>iShudan :: main menu</h1>
<p>Hello, <?php echo $_SESSION['username']; ?>.  Please select one of your games from the list below.</p>
<div class="center"><a href="create.php">create game</a> <a href="settings.php">settings</a> <a href="index.php?logout">log out</a></div>
<form method="post" action="index.php" name="playform">
<input type="hidden" name="game_id" value="0" />
<table class="game">
	<caption>your games</caption>
	<tr>
		<th>game id</th>
		<th>black player</th>
		<th>white player</th>
		<th>board size</th>
		<th>moves</th>
		<th>turn</th>
	</tr><?php

if (is_array($games) && (0 != count($games)))
{
	$i = 0;
	foreach ($games as $game)
	{
		$turn = '';

		if ((($_SESSION['username'] == $game['black_username']) && ('Black' == $game['g_status']))
			|| (($_SESSION['username'] == $game['white_username']) && ('White' == $game['g_status'])))
		{
			$turn = ' turn';
		}

		$alt = (0 == ($i % 2)) ? ' alt' : '';

		$class = (('' != $alt) || ('' != $turn)) ? " class=\"{$turn}{$alt}\"" : '';

		$html = "<tr{$class} onclick=\"loadgame({$game['g_id']});\">
		<td>{$game['g_id']}</td>
		<td>{$game['black_username']}</td>
		<td>{$game['white_username']}</td>
		<td>{$game['g_board_size']}</td>
		<td>{$game['g_num_moves']}</td>
		<td>{$game['g_status']}</td>
	</tr>";

		echo $html;

		++$i;
	}
}
else
{
	?><tr><td colspan="6">You have no games listed</td></tr><?php
}

	?>
</table>
</form>
<form method="post" action="index.php" name="viewform">
<input type="hidden" name="game_id" value="0" />
<input type="hidden" name="spec" value="0" />
<table class="game">
	<caption>other games</caption>
	<tr>
		<th>game id</th>
		<th>black player</th>
		<th>white player</th>
		<th>board size</th>
		<th>moves</th>
		<th>turn</th>
	</tr><?php

if (is_array($other_games) && (0 != count($other_games)))
{
	$i = 0;
	foreach ($other_games as $game)
	{
		$alt = (0 == ($i % 2)) ? ' alt' : '';

		$class = ('' != $alt) ? " class=\"{$alt}\"" : '';

		$html = "<tr{$class} onclick=\"viewgame({$game['g_id']});\">
		<td>{$game['g_id']}</td>
		<td>{$game['black_username']}</td>
		<td>{$game['white_username']}</td>
		<td>{$game['g_board_size']}</td>
		<td>{$game['g_num_moves']}</td>
		<td>{$game['g_status']}</td>
	</tr>";

		echo $html;

		++$i;
	}
}
else
{
	?><tr><td colspan="6">There are no other games listed</td></tr><?php
}

	?>
</table>
<table class="game">
	<caption>finished games</caption>
	<tr>
		<th>game id</th>
		<th>black player</th>
		<th>white player</th>
		<th>board size</th>
		<th>moves</th>
		<th>turn</th>
	</tr><?php

if (is_array($finished_games) && (0 != count($finished_games)))
{
	$i = 0;
	foreach ($finished_games as $game)
	{
		$alt = (0 == ($i % 2)) ? ' alt' : '';

		$class = ('' != $alt) ? " class=\"{$alt}\"" : '';

		$html = "<tr{$class} onclick=\"viewgame({$game['g_id']});\">
		<td>{$game['g_id']}</td>
		<td>{$game['black_username']}</td>
		<td>{$game['white_username']}</td>
		<td>{$game['g_board_size']}</td>
		<td>{$game['g_num_moves']}</td>
		<td>{$game['g_status']}</td>
	</tr>";

		echo $html;

		++$i;
	}
}
else
{
	?><tr><td colspan="6">There are no finished games listed</td></tr><?php
}

	?>
</table>
</form>
<div id="footer">iShudan v<?php echo $VERSION; ?> - last updated on <?php echo $MOD_DATE; ?><br />
this program is free software released under the GPL and is based on phpGo available on SourceForge<br />
Join our Google Code Project: <a href="http://code.google.com/p/ishudan/">http://code.google.com/p/ishudan/</a><br />
Download a copy of this program and source code: <a href="http://code.google.com/p/ishudan/source">http://code.google.com/p/ishudan/source</a></div>
<?php call('GLOBALS'); ?>
</body>
</html>
